/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.model;

import L.L;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author gob
 */
public class StreamContainer extends InputStream implements Serializable {

    protected byte[] streamData;
    protected int dataSize = 0;
    protected int pos = 0;
    protected final int LEN = 32768;

    public StreamContainer(InputStream is) {
        int c;
        int totalSize = 0;
        ArrayList<byte[]> list = new ArrayList<byte[]>();
        byte buf[];

        buf = new byte[LEN]; // for first round
        try {
            while ((c = is.read(buf)) != -1) {
                if (c > 0) {
                    //byte smallerbuf[] = new byte[c];
                    //System.arraycopy(buf, 0, smallerbuf, 0, c);
                    //list.add(smallerbuf.clone());
                    list.add(new byte[c]);
                    System.arraycopy(buf, 0, list.get(list.size() - 1), 0, c);
                }
                totalSize += c;

                // buffer for next round
                buf = new byte[LEN];

            }
        } catch (IOException iOException) {
            L.e("ioException beim buffer einlesen", iOException);
        }
        c = 0;
        streamData = new byte[totalSize];
        for (byte[] ba : list) {
            // for (int i = 0; i < ba.length; i++) {
            //     streamData[i + c] = ba[i];

            // }
            System.arraycopy(ba, 0, streamData, c, ba.length);
            c += ba.length;
        }
        L.o("totalSize, length", totalSize, streamData.length);
        System.out.println("------------------- internal -----------------");
        System.out.write(streamData, 0, totalSize);
        dataSize = totalSize;
        buf = null;
    }

    @Override
    public int read() throws IOException {
        if (pos < dataSize) {
            int data = (int)(streamData[pos] & 0xFF);

            pos++;
            return data;
        } else {
            return -1;
        }
    }


    @Override
    public int available() throws IOException {
        return dataSize - pos;
    }

    public static void main(String[] args) throws FileNotFoundException {

        StreamContainer mys;
        try {
            mys = new StreamContainer(new FileInputStream("/Users/gob/Dok/subwaysubdestages.jpg"));
            System.out.println(mys.available());

            byte[] b = new byte[mys.available()];
            int readnum = mys.read(b);
            System.out.println("------------------- externternal -----------------");
            System.out.write(b, 0, b.length);

        } catch (Exception fileNotFoundException) {
            System.out.println("filenotfound");
        }//System.out.write(m);

    }
    
    @Override
    public String toString() {
        return "";
    }
}
